package com.dgut.finaldemo.controller;

import com.dgut.finaldemo.entity.Teacher;
import com.dgut.finaldemo.service.TeacherService;
import com.dgut.finaldemo.util.R;
import com.dgut.finaldemo.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/teacher")
    @ResponseBody
    public DataVO teacher(Integer page,Integer limit){
        return teacherService.findData(page, limit);
    }

    @GetMapping("/teacherHtml")
    public String teacherHtml(){
        return "teacher";
    }

    @RequestMapping("/addTeacher")
    public String addTeacher(){
        return "addTeacher";
    }

    @RequestMapping("teacher/addTeacherSubmit")
    @ResponseBody
    public R addTeacherSubmit(Teacher teacher){
        if(teacher.getStatus()==null){
            teacher.setStatus(0);
        }
        teacherService.addTeacher(teacher);
        return R.ok();
    }

    @RequestMapping("teacher/updateTeacherSubmit")
    @ResponseBody
    public R updateTeacherSubmit(Teacher teacher){
        if(teacher.getStatus()==null){
            teacher.setStatus(0);
        }else {
            teacher.setStatus(1);
        }
        teacherService.updateTeacher(teacher);
        return R.ok();
    }

    @RequestMapping("teacher/deleteTeacherById")
    @ResponseBody
    public R deleteTeacherById(Integer tid){
        teacherService.deleteTeacherById(tid);
        return R.ok();
    }

    @RequestMapping("teacher/searchTeacher")
    @ResponseBody
    public DataVO searchTeacher(Integer page,Integer limit,Teacher teacher){
        System.out.println(page);
        System.out.println(limit);
        System.out.println(teacher);
        return teacherService.searchTeacher(page, limit, teacher);
    }

}
